package com.turi.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.turi.dto.LabelCreateByChanCategoryDTO;
import com.turi.vo.CallBackVO;
import com.turi.vo.ResultVO;
import lombok.AllArgsConstructor;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import org.springframework.web.bind.annotation.*;

import java.util.Random;

/**
 * @author Administrator
 */
@RestController
@AllArgsConstructor
@RequestMapping("/test")
public class TestController {

    private final ObjectMapper objectMapper;

    @PostMapping("/createLabel")
    public ResultVO createLabel(@RequestBody LabelCreateByChanCategoryDTO dto) {


        MediaType mediaType = MediaType.parse("application/json; charset=utf-8");

        //处理请求json
        String json = null;
        try {
            json = objectMapper.writeValueAsString(dto);
        } catch (JsonProcessingException e) {
            throw new RuntimeException(e);
        }

        OkHttpClient client = new OkHttpClient().newBuilder()
                .build();

        okhttp3.RequestBody requestBody = okhttp3.RequestBody.create(mediaType, json);

        Request request = new Request.Builder()
                .url("http://192.168.40.41:8888/v1/label-create/asyncCreateByChanCategory")
                .method("POST", requestBody)
                .addHeader("Content-Type", "application/json")
                .addHeader("Authorization", "eyJhbGciOiJIUzI1NiJ9.eyJyb2xlSWQiOiIxMTIzNTk4ODE2NzM4Njc1MjAyIiwic2xzVXNlcklkIjoxOTQwNjY1MTQzNTUyOTg3MTM3LCJibGFkZVVzZXJJZCI6MTk0MDY2NTEzNTUxMjUwNjM3MCwiaWF0IjoxNzYxMzE1NTA5LCJleHAiOjE3NzY4Njc1MDl9.pwhUVrds_HFXvOdwuZbUEtNccaTz9S6X844VUj1RoN0")
                .build();

        try (Response response = client.newBuilder().build()
                .newCall(request).execute()) {
            if (response.body() != null) {
                System.out.println("response = " + response.body().string());
            }
            return ResultVO.success();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }

    }

    /**
     * 回调
     * @param msg
     * @return
     */
    @PostMapping("/callBack")
    public ResultVO<CallBackVO> callBack(String msg){

        Random random = new Random();
        int i = random.nextInt();
        if (i % 2 == 0){
            CallBackVO callBackVO = new CallBackVO();
            //msg
            callBackVO.setMsg("消息已确认");
            //isSuccess
            callBackVO.setIsSuccess("1");
            System.out.println("msg = " + callBackVO);
            return ResultVO.success(callBackVO);
        }else {
            CallBackVO callBackVO = new CallBackVO();
            //msg
            callBackVO.setMsg("阿巴阿巴");
            //isSuccess
            callBackVO.setIsSuccess("1");
            System.out.println("msg = " + callBackVO);
            return ResultVO.success(callBackVO);
        }

    }




}
